package com.thinkfit.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.common.lib.utils.DensityUtil;
import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.ImageLoadUtils;

import java.util.List;

public class GridViewAdapter extends BaseAdapter {

    private List<AppUtils.AppInfo> appList;

    private LayoutInflater layoutInflater;

    //当前页索引
    private int currentIndex;
    Context context;


    //占满屏幕时每页展示的主题个数
    private int pageSize;

    public GridViewAdapter(Context context, List<AppUtils.AppInfo> appList, int currentIndex, int pageSize) {
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.appList = appList;
        this.currentIndex = currentIndex;
        this.pageSize = pageSize;
    }

    /**
     * 如果剩余数据能够完全占满当前页，则返回 pageSize
     * 如果不能，则返回剩余的数据个数
     */
    @Override
    public int getCount() {
        return appList.size() > (currentIndex + 1) * pageSize ? pageSize : (appList.size() - currentIndex * pageSize);
    }

    /**
     * 计算出正确的索引
     */
    @Override
    public Object getItem(int position) {
        return appList.get(position + currentIndex * pageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + currentIndex * pageSize;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_app_popup_window, parent, false);
            viewHolder = new ViewHolder();
//            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
//            viewHolder.ivUninstall = (ImageView) convertView.findViewById(R.id.ivUninstall);
//            viewHolder.ll_more = (LinearLayout) convertView.findViewById(R.id.ll_more);
//            viewHolder.shaderView = (View) convertView.findViewById(R.id.shaderView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int pos = position + currentIndex * pageSize;
//        viewHolder.ivUninstall.setVisibility(View.GONE);
//        viewHolder.ll_more.setVisibility(View.GONE);
//        viewHolder.shaderView.setVisibility(View.GONE);
//        viewHolder.tvName.setText(appList.get(pos).getName());
        ImageLoadUtils.getInstance(context).setImageRadius(viewHolder.ivIcon,appList.get(pos).getIcon(), 22);
        viewHolder.tvName.setText(appList.get(pos).getName());
        return convertView;
    }

    private class ViewHolder {

//        private TextView tvName;

        private ImageView ivIcon;
        private TextView tvName;
//        private ImageView ivUninstall;
//        private LinearLayout ll_more;
//        private View shaderView;

    }

}
